package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwahT;
import cn.e4j.modular.crm.entity.GwahlT;
import cn.e4j.modular.crm.entity.Gwai250Export;
import cn.e4j.modular.crm.service.GwahTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产品分类设置管理
 *
 * @author shixc
 * @Date 2018/01/09
 */
@Controller
@RequestMapping("/crm/gwai250")
public class Gwai250Controller extends BaseController {

    @Autowired
    private GwahTService gwahTService;

    /**
     * 跳转产品分类设置页面
     */
    @RequiresPermissions("crm:gwai250:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai250.html";
    }


    /**
     * 查询产品分类信息列表
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAHENT", user.getDBC());
        List<Map<String, Object>> result = gwahTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai250:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();

        //判断产品分类编号是否重复
        GwahT gwaht = this.gwahTService.getByKey(inDto.getString("GWAH001"));
        if (gwaht != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWAH.getCode(),
                    CrmExceptionEnum.EXISTED_THE_GWAH.getMessage());
        }
        GwahT gwah = new GwahT();
        gwah.setGWAH001(inDto.getString("GWAH001"));
        gwah.setGWAH002(" ");
        gwah.setGWAH003(" ");
        gwah.setGWAHACTI(inDto.getString("GWAHACTI"));
        gwah.setGWAHENT(user.getDBC());
        gwah.setGWAHUSER(user.getACCOUNT());
        gwah.setGWAHGRUP(user.getORG_ID());
        Date date = new Date();
        gwah.setGWAHTIME(DateUtils.formatDate(date, "HH:mm:ss"));
        LocalDate today = LocalDate.now();
        gwah.setGWAHDATE(today);
        gwah.setGWAHORIG(user.getORG_ID());
        gwah.setGWAHORIU(user.getACCOUNT());
        GwahlT gwahl = new GwahlT();
        gwahl.setGWAHLENT(user.getDBC());
        gwahl.setGWAHL001(gwah.getGWAH001());
        gwahl.setGWAHL002(LangUtils.getLang(user.getLANG()));
        gwahl.setGWAHL003(inDto.getString("GWAHL003"));
        gwahl.setGWAHL004(inDto.getString("GWAHL004"));
        gwahTService.add(gwah, gwahl);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai250:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        //判断产品分类编号是否存在
        GwahT gwah = this.gwahTService.getByKey(inDto.getString("GWAH001"));
        if (gwah == null) {
            return new ErrorResponseData(CrmExceptionEnum.EMPTY_THE_GWAH.getCode(),
                    CrmExceptionEnum.EMPTY_THE_GWAH.getMessage());
        }
        inDto.put("GWAHENT", user.getDBC());
        inDto.put("GWAHMODU", user.getACCOUNT());
        gwahTService.edit(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai250:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAHENT", user.getDBC());
        gwahTService.del(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai250:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwahT> gwahTList = inDto.getReqList(GwahT.class);
        gwahTService.deleteDataBatch(gwahTList);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/exportsData", method = RequestMethod.GET)
    @RequiresPermissions("crm:gwai250:init")
    public void exportsData(ModelMap map, HttpServletRequest request, HttpServletResponse response,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();
        inDto.put("lang", LangUtils.getLang(user.getLANG()));
        inDto.put("GWAHENT", user.getDBC());
        List<Map<String, Object>> list = gwahTService.getList(null, inDto);
        ExportParams params = new ExportParams(null, "产品分类数据", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "产品分类数据");
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, Gwai250Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }


}
